//
//  RBCreateDataBaseTabel.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/8.
//

#import "RBCreateDataBaseTabel.h"
#import "RBDataBaseTool.h"
#import "FMDatabase.h"

#define kDataBaseVersion  1
static NSString *dbBase_info = @"create table if not exists dbBaseVersion_info (d_key text primary key, d_value text)";
//苹果内购储存的单号
static NSString *table_iap_list = @"CREATE TABLE IF NOT EXISTS tab_iap(receipt_data TEXT NOT NULL, order_id text not null ,money text not null ,order_sn text not null,transaction_id text not null,user_id text not null,primary key(transaction_id))";

@implementation RBCreateDataBaseTabel
-(void)rb_createDBTabel{
    [[RBDataBaseTool shareInstance] rb_initDataBase:^(FMDatabase * _Nonnull db) {
        BOOL res = ([db executeUpdate:dbBase_info] &&[db executeUpdate:table_iap_list]);
        if (!res) {
        } else {
        }
    }];
}
-(void)rb_compareDBVersion{
    // 检查是否存在升级表, 不存在则初始化数据库表。初始安装
    if (![self rb_isTableOK:@"dbBaseVersion_info"]) {
        [self rb_createDBTabel];
        [self rb_dBInfoValueWithKey:@"db_version" value:[NSString stringWithFormat:@"%ld",(long)kDataBaseVersion]];
        return;
    }
    
    //升级安装
    NSInteger dbVersion = 0;
    id result = [self rb_getDBInfoValueWithKey:@"db_version" value:nil];
    dbVersion = [result integerValue];
    
    if (dbVersion != kDataBaseVersion) {
        [self rb_upgradeDBWithVersion:dbVersion _newversion:kDataBaseVersion];
    }
}
- (BOOL)rb_isTableOK:(NSString *)tablename {
    __block NSInteger _count = 0;
    [[RBDataBaseTool shareInstance] rb_initDataBase:^(FMDatabase * _Nonnull db) {
        FMResultSet *rs = [db executeQuery:@"select count(*) as 'count' from sqlite_master where type = 'table' and name = ?",tablename];
        while ([rs next]) {
            _count = [rs intForColumn:@"count"];
        }
    }];
    if (_count == 0) {
        return NO;
    }else{
        return YES;
    }
    return NO;
}
- (BOOL)rb_upgradeDBWithVersion:(NSInteger)oldversion _newversion:(NSInteger)_newversion {
    for (NSInteger currentVersion = oldversion; currentVersion < _newversion; currentVersion++) {
        switch (currentVersion) {
            case 0:
            {
                [[RBDataBaseTool shareInstance] rb_initDataBase:^(FMDatabase * _Nonnull db) {
                    BOOL res = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS tab_iap(receipt_data TEXT NOT NULL, order_id text not null ,money text not null ,order_sn text not null,transaction_id text not null,user_id text not null,primary key(transaction_id))"];
                    if (!res) {
                        NSLog(@"error when creating message_table error : %@",[db lastError]);
                    }
                }];
                    break;
            }
            default:
                break;
        }
    }
    [self rb_dBInfoValueWithKey:@"db_version" value:[NSString stringWithFormat:@"%ld",(long)_newversion]];
    
    return YES;
}

-(BOOL)rb_dBInfoValueWithKey:(NSString *)key value:(NSString *)value {
    id resultTmp = [self rb_getDBInfoValueWithKey:key value:value];
    if (resultTmp != NULL) {
        // 存在
        [self rb_updateDBInfoValueWithKey:key value:value];
    } else {
        // 不存在
        [self rb_insertDBInfoValueWithKey:key value:value];
    }
    return YES;
}
//得到数据库版本号
- (id)rb_getDBInfoValueWithKey:(NSString *)key value:(NSString *)value {
    __block id result;
    [[RBDataBaseTool shareInstance] rb_initDataBase:^(FMDatabase * _Nonnull db) {
        FMResultSet *rs = [db executeQuery:@"select * from dbBaseVersion_info where d_key = ?",key];
        while ([rs next]) {
            result = [rs stringForColumn:@"d_value"];
        }
    }];
    return result;
}
- (BOOL)rb_updateDBInfoValueWithKey:(NSString *)key value:(NSString *)value {
    __block BOOL _result;
    [[RBDataBaseTool shareInstance] rb_initDataBase:^(FMDatabase * _Nonnull db) {
        BOOL result = [db executeUpdate:@"update dbBaseVersion_info set d_key = ?, d_value = ?",key,value];
        if (!result) {
            _result = NO;
        } else {
            _result = YES;
        }
    }];
    return _result;
}
-(BOOL)rb_insertDBInfoValueWithKey:(NSString *)key value:(NSString *)value {
    __block BOOL _result;
    [[RBDataBaseTool shareInstance] rb_initDataBase:^(FMDatabase * _Nonnull db) {
        BOOL result = [db executeUpdate:@"INSERT INTO dbBaseVersion_info (d_key, d_value) VALUES (?,?)",key,value];
        if (!result) {
            _result = NO;
        } else {
            _result = YES;
        }
    }];
    return _result;
}
@end
